package newcrunch.typography;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author WL
 */
public class SpaceProperty 
{
    private List<KeyRecord> spaceBefore;
    private List<KeyRecord> spaceAfter;
    private List<KeyRecord> newlineBefore;
    private List<KeyRecord> newlineAfter;
    private String name;
        
    public SpaceProperty(String name)
    {
        this.name = name;
        spaceBefore = new ArrayList<KeyRecord>();
        spaceAfter = new ArrayList<KeyRecord>();
        newlineBefore = new ArrayList<KeyRecord>();
        newlineAfter = new ArrayList<KeyRecord>();
    }
    
    public boolean isNonEmpty()
    {
        return spaceBefore.size() > 0 || spaceAfter.size() > 0 ||
                newlineBefore.size() > 0 || newlineAfter.size() > 0;
    }
    
    @Override
    public String toString()
    {
        StringBuffer result = new StringBuffer(name + ": \n");
        
        if (spaceBefore.size() > 0)
        {
            result.append("space before: ");
            for (KeyRecord pair : spaceBefore)
                result.append(pair + " ");
            result.append("\n");
        }
        
        if (spaceAfter.size() > 0)
        {
            result.append("space after: ");
            for (KeyRecord pair : spaceAfter)
                result.append(pair + " ");
            result.append("\n");
        }
        
        if (newlineBefore.size() > 0)
        {
            result.append("newline before: ");
            for (KeyRecord pair : newlineBefore)
                result.append(pair + " ");
            result.append("\n");
        }
        
        if (newlineAfter.size() > 0)
        {
            result.append("newline after: ");
            for (KeyRecord pair : newlineAfter)
                result.append(pair + " ");
            result.append("\n");
        }
        
        return result.toString();
    }
    
    public void addSpaceAfter(int pos, int line, int column, int spaces)
    {        
        spaceAfter.add(new KeyRecord(pos, line, column, spaces));
    }
    
    public void addSpaceBefore(int pos, int line, int column, int spaces)
    {
        spaceBefore.add(new KeyRecord(pos, line, column, spaces));
    }
    
    public void addNewlineBefore(int pos, int line, int column, int spaces)
    {
        newlineBefore.add(new KeyRecord(pos, line, column, spaces));
    }
    
    public void addNewlineAfter(int pos, int line, int column, int spaces)
    {
            newlineAfter.add(new KeyRecord(pos, line, column, spaces));
    }
    
    /**
     * Get the list of (position, spaces) where this identifier is found with.
     * @return 
     */
    public List<KeyRecord> getSpaceAfter()
    {
        return spaceAfter;
    }
    
    /**
     * Get the list of (position, spaces) where this identifier is found with.
     * @return 
     */
    public List<KeyRecord> getSpaceBefore()
    {
        return spaceBefore;
    }
}
